﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Evan.Template
{
    /// <summary>
    /// 模板用到的相关功能 , 包含获取栏目的一些通用方法
    /// </summary>
    public static class TemplateHelper
    {
        /// <summary>
        /// 枚举 对于模板类型目录
        /// </summary>
        public enum PageTypeEnum {
            /// <summary>
            /// 
            /// </summary>
            Integrated = 0,
            /// <summary>
            /// 文章
            /// </summary>
            Article = 1,
            /// <summary>
            /// 产品
            /// </summary>
            Product = 2,
            /// <summary>
            /// 单页
            /// </summary>
            SinglePage = 3, 
            /// <summary>
            /// 数据调用
            /// </summary>
            Banner = 4, 
            /// <summary>
            /// 案例
            /// </summary>
            Case = 5,
            /// <summary>
            /// 解决方案
            /// </summary>
            Solution = 6,
            /// <summary>
            /// 视频
            /// </summary>
            Video = 7,
            /// <summary>
            /// 下载
            /// </summary>
            DownLoad = 8,
            /// <summary>
            /// faq
            /// </summary>
            FAQ = 9,
            /// <summary>
            /// 留言
            /// </summary>
            FeedBack = 10,
            /// <summary>
            /// 招聘
            /// </summary>
            Job = 11,
            /// <summary>
            /// 友情链接
            /// </summary>
            Link = 12, 
            /// <summary>
            /// 会员
            /// </summary>
            Member = 13,
            /// <summary>
            /// 图册
            /// </summary>
            Picture = 14,
        }


        /// <summary>
        /// 根据栏目类型获取表名称
        /// </summary>
        /// <param name="PageType"></param>
        /// <returns></returns>
        public static string GetTableNameByPageType(int PageType)
        {
            string TableName = "";
            switch (PageType)
            {
                case 0:
                    TableName = "";
                    break;
                case 1:
                    TableName = "Article";
                    break;
                case 2:
                    TableName = "Product";
                    break;
                case 3:
                    TableName = "SinglePage";
                    break;
                case 4:
                    TableName = "Banner";
                    break;
                case 5:
                    TableName = "Cases";
                    break;
                case 6:
                    TableName = "Solution";
                    break;
                case 7:
                    TableName = "Video";
                    break;
                case 8:
                    TableName = "DownLoad";
                    break;
                case 9:
                    TableName = "Faq";
                    break;
                case 10:
                    TableName = "FeedBack";
                    break;
                case 11:
                    TableName = "Job";
                    break;
                case 12:
                    TableName = "Links";
                    break;
                case 13:
                    TableName = "Member";
                    break;
                case 14:
                    TableName = "Picture";
                    break;
                default:
                    break;
            }
            return TableName;
        }


        /// <summary>
        /// 获取排序字段id 对应的值  改为单独获取
        /// 仅限此类内部调用 , 为了解决排序相同或者相反的问题
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        private static string GetOrderStr(int order)
        {
            string sqlOrderStr = "";
            switch (order)
            {
                case 0:
                    sqlOrderStr = "AddTime desc";
                    break;
                case 1:
                    sqlOrderStr = "AddTime asc";
                    break;
                case 2:
                    sqlOrderStr = "SortId desc";
                    break;
                case 3:
                    sqlOrderStr = "SortId asc";
                    break;
                default:
                    sqlOrderStr = "AddTime desc";
                    break;
            }
            return sqlOrderStr;
        }

        /// <summary>
        /// 获取排序SQL  asc 升序   desc 降序 
        /// </summary>
        /// <param name="order">栏目主排序  </param>
        /// <param name="suborder">栏目副排序</param>
        /// <returns></returns>
        public static string GetSqlOrderStr(int order, int suborder)
        {
            string sqlStr = "";
            sqlStr = GetOrderStr(order);
            if (order != suborder)
            {
                string _sqlStr = GetOrderStr(suborder);

                if (sqlStr.Split(' ')[0].ToString() == _sqlStr.Split(' ')[0].ToString())
                {
                    //前缀一样 , 排序只能用一个
                    sqlStr = " " + sqlStr + " ";
                }
                else
                {
                    //前缀不一样 , 两个排序都可用
                    sqlStr = " " + sqlStr + " , " + _sqlStr + " ";
                }
            }
            return sqlStr;
        }

        /// <summary>
        /// 获取排序SQL  asc 升序   desc 降序 
        /// 当前栏目
        /// </summary>
        /// <param name="curChannel">传入当前栏目</param>
        /// <returns></returns>
        public static string GetSqlOrderStr(dynamic curChannel)
        {
            return GetSqlOrderStr(Convert.ToInt32(curChannel.OrderBy), Convert.ToInt32(curChannel.SubOrderBy));
        }

        /// <summary>
        /// 获取排序SQL  asc 升序   desc 降序 
        /// </summary>
        /// <param name="ClassId">栏目ID  </param>
        /// <returns></returns>
        public static string GetSqlOrderStr(int ClassId)
        {
            dynamic Channel = new DataBase.DbOperat("Channel").GetModel(ClassId);
            return GetSqlOrderStr(Convert.ToInt32(Channel.OrderBy), Convert.ToInt32(Channel.SubOrderBy));
        }

        /// <summary>
        /// 获取相册的图片列表
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public static List<Model.SysModel.PhotoModel> GetPhoto(string jsonStr)
        {
            if (string.IsNullOrEmpty(jsonStr))
            {
                return new List<Model.SysModel.PhotoModel>();
            }
            else
            {
                return Common.JsonHelper.JsonToList<Model.SysModel.PhotoModel>(jsonStr);
            }
        }


        /// <summary>
        /// 获取PageType的字典
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> PageTypeDic()
        {
            Dictionary<string, string> PageType = new Dictionary<string, string>();
            PageType.Add("0", "综合页面");
            PageType.Add("1", "新闻");
            PageType.Add("2", "产品");
            PageType.Add("3", "单页");
            PageType.Add("4", "Banner"); //数据调用
            PageType.Add("5", "案例");
            PageType.Add("6", "解决方案");
            PageType.Add("7", "视频");
            PageType.Add("8", "下载");
            PageType.Add("9", "FAQ");
            PageType.Add("10", "留言");
            PageType.Add("11", "招聘");
            PageType.Add("12", "友情链接"); //数据调用                                                                        
            PageType.Add("13", "会员");
            PageType.Add("14", "图册");
            PageType.Add("15", "外链");
            PageType.Add("20", "自定义管理页面");
            return PageType;
        }

        /// <summary>
        /// 获取PageType的字典
        /// <para>仅适用于模块系统</para>
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> PageTypeDicForModule()
        {
            Dictionary<string, string> PageType = new Dictionary<string, string>();
            PageType.Add("0", "综合页面（页面为空）");
            PageType.Add("1", "新闻");
            PageType.Add("2", "产品");
            PageType.Add("3", "单页");
            PageType.Add("4", "Banner"); //数据调用
            return PageType;
        }

        /// <summary>
        /// 扩展Dic , 扩展的必须从100 开始计算ID , 否则会报错
        /// </summary>
        /// <param name="AddDic">扩展的字典  AddDic.Add("100", "自定义栏目类型"); </param>
        /// <returns></returns>
        public static Dictionary<string, string> PageTypeDic(Dictionary<string, string> AddDic)
        {
            Dictionary<string, string> PageType = new Dictionary<string, string>();
            PageType.Add("0", "综合页面");
            PageType.Add("1", "新闻");
            PageType.Add("2", "产品");
            PageType.Add("3", "单页");
            PageType.Add("4", "Banner"); //数据调用
            PageType.Add("5", "案例");
            PageType.Add("6", "解决方案");
            PageType.Add("7", "视频");
            PageType.Add("8", "下载");
            PageType.Add("9", "FAQ");
            PageType.Add("10", "留言");
            PageType.Add("11", "招聘");
            PageType.Add("12", "友情链接"); //数据调用                                                                        
            PageType.Add("13", "会员");
            PageType.Add("14", "图册");
            PageType.Add("15", "外链");
            PageType.Add("20", "自定义管理页面");

            if (AddDic.Keys.Count>0)
            {
                foreach (var item in PageType)
                {
                    PageType.Add(item.Key, item.Value);
                }
            }

            return PageType;
        }


        /// <summary>
        /// 获取动态Model中某一字段的值
        /// </summary>
        /// <param name="Model">动态类</param>
        /// <param name="FieldName">字段名称</param>
        /// <returns>如果值不存在则返回 空('') ,  注意 : 并非 null</returns>
        public static string GetField(dynamic Model,string FieldName)
        {
            Dictionary<string,object> Dic = Evan.Model.ConvertModel.ConvertToDictionary(Model);
            if (Dic.Keys.Contains(FieldName))
            {
                if (Dic[FieldName] != null)
                {
                    return Dic[FieldName].ToString();
                }
                else
                {
                    return "";
                }
            }
            else
            {
                return ""; 
            }
        }

    }
}
